<?php

/*

	MMetaverse - Open Metaverse Access Library

	Copyright (c) 2009 Mary Jane Soft - Marlon J. Manrique

	http://www.maryjanesoft.com
	http://www.marlonj.com

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General
	Public License along with this library; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place, Suite 330,
	Boston, MA  02111-1307  USA

	$Id$

*/

/**
 * @package mjs.mmetaverse.manager
 */

/**
 * 
 * Asset Manager
 */
class MAssetManager {

    /**
     * DataBase
     */
    private $db;

    /**
     * Create a Asset Manager
     *
     * @param MMetaverse $metaverse Metaverse
     */
    function MAssetManager(MMetaverse $metaverse) {
        $this->db = $metaverse->getDataBase('asset');
    }

    /**
     * Return the image with the uuid specified
     *
     * @param MUUID $uuid UUID of the image
     */
    public function getImageData(MUUID $uuid) {
        return $this->db->getImageData($uuid);
    }

    /**
     * Save the image into the file specified 
     *
     * @param MUUID $uuid The image uuid
     * @param string $filename The name of the image file
     * @return 
     */
    public function saveImage(MUUID $uuid, $filename) {
        // Get the Image data
        $imageData = $this->getImageData($uuid);
        
        // Create the imagick object to convert jp2 to png
        $image = new Imagick();

        // Read the image
        $image->readImageBlob($imageData);

        // Put the image format according extension 
        $info = pathinfo($filename);
        $image->setImageFormat($info['extension']);
        $image->writeImage($filename);
    }

    /**
     * Send the image to the client 
     *
     * @param MUUID $uuid The image uuid
     * @param string $format Image format
     * @return
     */
    public function showImage(MUUID $uuid, $format) {
        // Get the Image data
        $imageData = $this->getImageData($uuid);

        // Create the imagick object to convert jp2 to png
        $image = new Imagick();

        // Read the image
        $image->readImageBlob($imageData);

        // Put the image format according extension
        $info = pathinfo($fileName);
        $image->setImageFormat($format);
        $output = $image->getImageBlob();
        $type = $image->getFormat();

        // Send to the client 
        header("Content-type: $type");
        echo $output;
    }

    /**
     * Returns the asset with the given uuid
     *
     * @param MUUID $uuid asset uuid
     * @return MAsset
     */
    public function getAsset(MUUID $uuid) {
        return $this->db->getAsset($uuid);
    }

    /**
     * Add new asset
     *
     * @param MAsset $asset Add new asset
     */
    public function addAsset(MAsset $asset) {
        return $this->db->addAsset($asset);
    }
}

?>
